package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.tanhua.dubbo.server.vo.UserLocationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/3/1 20:12
 * @version: 0.0.1
 * @modified By:
 */
@Service(version = "2.0.0")
public class ESUserLocationApiImpl implements UpdateUserLocationApi {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${es.server.url}")
    private String esServerUrl;

    private static final ObjectMapper MAPPER=new ObjectMapper();

    @Override
    public String updateUserLocation(Long userId, Double longitude, Double latitude, String address) {
        try {
            String url=this.esServerUrl+"user/location/";
            Map<String,Object>param=new HashMap<>();
            param.put("userId",userId);
            param.put("longitude",longitude);
            param.put("latitude",latitude);
            param.put("address",address);
            String requestBody = MAPPER.writeValueAsString(param);
            HttpHeaders httpHeaders=new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String>httpEntity=new HttpEntity<>(requestBody,httpHeaders);
            ResponseEntity<Void> responseEntity = this.restTemplate.postForEntity(url, httpEntity, Void.class);
            if(responseEntity.getStatusCodeValue()==200){
                return userId.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public UserLocationVo queryByUserId(Long userId) {
        String url=this.esServerUrl+"user/location/"+userId;
        ResponseEntity<String> responseEntity = this.restTemplate.getForEntity(url, String.class);
        if(responseEntity.getStatusCodeValue()!=200){
            return null;
        }
        try {
            String body=responseEntity.getBody();
            JsonNode jsonNode=MAPPER.readTree(body);
            UserLocationVo userLocationVo=new UserLocationVo();
            userLocationVo.setLongitude(jsonNode.get("location").get("lon").asDouble());
            userLocationVo.setLatitude(jsonNode.get("location").get("lat").asDouble());
            userLocationVo.setUserId(userId);
            userLocationVo.setCreated(jsonNode.get("created").asLong());
            userLocationVo.setUpdated(jsonNode.get("updated").asLong());
            userLocationVo.setLastUpdated(jsonNode.get("lastUpdated").asLong());
            userLocationVo.setAddress(jsonNode.get("address").asText());
            userLocationVo.setId(userId.toString());
            return userLocationVo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<UserLocationVo> queryUserFromLocation(Double longitude, Double latitude, Integer range) {
        String url=this.esServerUrl+"user/location/list";
        Map<String,Object>param=new HashMap<>();
        param.put("longitude",longitude);
        param.put("latitude",latitude);
        param.put("distance",range);
        try {
            HttpHeaders httpHeaders=new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String>httpEntity=new HttpEntity<>(MAPPER.writeValueAsString(param),httpHeaders);
            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, httpEntity, String.class);
            if(responseEntity.getStatusCodeValue()!=200){
                return null;
            }
            String body=responseEntity.getBody();
            ArrayNode jsonNode= (ArrayNode) MAPPER.readTree(body);
            List<UserLocationVo>userLocationVoList=new ArrayList<>();
            for (JsonNode node : jsonNode) {
                UserLocationVo userLocationVo=new UserLocationVo();
                userLocationVo.setLongitude(node.get("location").get("lon").asDouble());
                userLocationVo.setLatitude(node.get("location").get("lat").asDouble());
                userLocationVo.setUserId(node.get("userId").asLong());
                userLocationVo.setCreated(node.get("created").asLong());
                userLocationVo.setUpdated(node.get("updated").asLong());
                userLocationVo.setLastUpdated(node.get("lastUpdated").asLong());
                userLocationVo.setAddress(node.get("address").asText());
                userLocationVo.setId(userLocationVo.getUserId().toString());
                userLocationVoList.add(userLocationVo);
            }
            return userLocationVoList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
